<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Banner extends Zend_Db_Table{
	
	protected $_name    = "banners";
    protected $_primary = "id";
    
    public function getDatas($options = null, $cols = array()){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $date = new Zend_Date();
        $curDate = $date->get("Y-M-d");
        
        if(count($cols) == 0){
            $cols = array('id', 'banner', 'image', 'position', 'link', 'status', 'start_date', 'end_date');
        }
        
        $select = $db->select()
                    ->from(array('b' => 'banners'), $cols)
                    ->where('? >= b.start_date', $curDate)
                    ->where('b.end_date >= ?', $curDate)
                    ->where('b.status = 1');  
                       
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('b.created DESC');
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($position){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        $date = new Zend_Date();
        $curDate = $date->get("Y-M-d");
        
        echo $select = $db->select()
                    ->from(array('b' => 'banners'), array('banner', 'image', 'link'))
                    ->order('b.created DESC')
                    ->where('? >= b.start_date', $curDate)
                    ->where('b.end_date >= ?', $curDate)
                    ->where('b.status = 1')
                    ->where('b.position = ?', $position)
                    ->limit(1);
        
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        $rs = $db->fetchRow($select);
        
        return $rs;
    }
}